package com.hk.zuulservice.handler;

import com.alibaba.fastjson.JSON;
import com.hk.commons.CommonResult;
import com.hk.model.vo.Result;
import com.netflix.hystrix.exception.HystrixTimeoutException;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author: aerfazhe
 * @Date: 2021/8/10 15:47
 * @Statement: 超时处理异常
 */
@Component
public class UnifyFallbackProvider implements FallbackProvider {

    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        if (cause instanceof HystrixTimeoutException) {
            return response(HttpStatus.GATEWAY_TIMEOUT);
        } else {
            return response(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private ClientHttpResponse response(final HttpStatus status) {
        return new ClientHttpResponse() {
            /**
             * 设置相应状态码 HttpStatus类型
             * @return
             * @throws IOException
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.INTERNAL_SERVER_ERROR;
            }

            /**
             * fallback的状态码
             * @return int类型
             * @throws IOException
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return this.getStatusCode().value();
            }

            /**
             * fallback的状态码
             * @return String类型
             * @throws IOException
             */
            @Override
            public String getStatusText() throws IOException {
                return this.getStatusCode().getReasonPhrase();
            }

            @Override
            public void close() {

            }

            /**
             * 设置响应体
             * @return
             * @throws IOException
             */
            @Override
            public InputStream getBody() throws IOException {
                Result result = new Result(this.getStatusCode().value(),this.getStatusCode().getReasonPhrase(),"服务器出走啦，马上回来...");
                return new ByteArrayInputStream(JSON.toJSONString(result).getBytes());
            }

            /**
             * 响应头信息
             * @return
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
//                headers.setContentType(new MediaType("application","json", Charset.forName("UTF-8")));
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }

}
